package com.ethink.platform.manager;

import cn.hutool.core.util.IdUtil;
import com.ethink.component.oss.OssClient;
import com.ethink.framework.common.constant.CommonRespCode;
import com.ethink.framework.common.exception.BusinessException;
import com.ethink.platform.common.properties.PlatformCommonProperties;
import com.ethink.system.api.SystemFileFacade;
import com.ethink.system.api.request.SystemFileAddRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author default
 * @version 1.0.0
 * @ClassName OSSTemplateManager.java
 * @Description
 * @createTime 2022年11月29日 15:18:00
 */
@Component
@Slf4j
public class OssTemplateManager {


    @Autowired
    private OssClient ossClient;

    @Autowired
    private PlatformCommonProperties commonProperties;

    /**
     * 文件路径
     */
    private final SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");


    @Autowired
    private SystemFileFacade systemFileFacade;


    public Map<String, String> upload(MultipartFile file, String bizType) {
        Map<String, String> response = new HashMap<>(8);
        String id = IdUtil.fastSimpleUUID();
        //文件名称生成
        String fileName = id + file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        //日期
        String datePath = this.format.format(new Date());
        //文件路径
        String filePath = commonProperties.getPrefix() + "/" + datePath + "/" + file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
        //上传
        try {
            Map<String, String> map = ossClient.upload(commonProperties.getBucketName(), file.getInputStream(), fileName, filePath);
            if (map.size() == 0) {
                throw new BusinessException(CommonRespCode.SYSTEM_ERR);
            }
            SystemFileAddRequest addRequest = new SystemFileAddRequest();
            addRequest.setFileId(id);
            addRequest.setBizType(bizType);
            addRequest.setFileName(fileName);
            addRequest.setUrl(map.get("url"));
            addRequest.setOssFileKey(map.get("key"));
            systemFileFacade.addSystemFile(addRequest);
            response.put("url", map.get("url"));
            response.put("fileId", id);
            return response;
        } catch (Exception e) {
            log.error("上传失败", e);
        }
        return null;
    }

    public String upload(ByteArrayOutputStream bos, String fileNamePrefix) {
        // 日期
        String datePath = this.format.format(new Date());
        String fileName = fileNamePrefix + "-" + IdUtil.getSnowflakeNextId() + ".xlsx";
        // 文件路径
        String filePath = commonProperties.getExportPrefix() + "/" + datePath;
        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
        Map<String, String> map = ossClient.upload(commonProperties.getBucketName(), bis, fileName, filePath);
        return map.get("url");
    }

    public List<Map<String, String>> batchUpload(MultipartFile[] file, String bizType) {
        List<Map<String, String>> urlList = new ArrayList<>();
        for (MultipartFile multipartFile : file) {
            urlList.add(upload(multipartFile, bizType));
        }
        return urlList;
    }
}
